package 剑指offer;

import java.util.Stack;

public class _30包含min函数的栈 {


}
/**
 * 
 * @author 兰伟朋
 *@time 2021年2月6日下午10:51:44
 *@see
 *	双栈实现
 */
class MinStack1 {

    /** initialize your data structure here. */
	Stack<Integer> A,B;
    public MinStack1() {
    		A=new Stack<Integer>();
    		B=new Stack<Integer>();
    }
    
    public void push(int x) {
    	A.push(x);
    	if(B.isEmpty()||B.peek()>=x) B.push(x);    		
    }
    
    public void pop() {
    	if(A.pop().equals(B.peek()))B.pop();
    }
    
    public int top() {
    	return A.peek();
    }
    
    public int min() {
    	return B.peek();
    }
}
class MinStack2 {

    /** initialize your data structure here. */
	int []stack=new int[10000];
	int min=Integer.MAX_VALUE;
	int index=0;
    public MinStack2() {
    	
    }
    
    public void push(int x) {
    	if(x<=min) {
    	stack[++index]=min;
    	}
    	min=x;
    	stack[++index]=x;
    	
    }
    
    public void pop() {
    	if(stack[index]==min)
    		min=stack[--index];
         index--;
    }
    
    public int top() {
    	return stack[index];
    }
    
    public int min() {
    	return min;
    }
}

